* --------------------- *
* This file loads mother and child level data to compute effects on fertility 
* and birth outcomes presented in Tables 4 and 5.
* Author: Victor Ronda
* Date Created: 01 Mar 2020
* Last Update: 22 Jan 2023
* --------------------- *

clear all
set more off

**************************
*** Fertility Measures ***
**************************
use "mother_data.dta", replace

keep if female==1

gen primip=0 if pregorder~=.
replace primip=1 if pregorder==1
egen numpreg_p1=rowmax(pregorder_ch1 pregorder_ch2 pregorder_ch3 pregorder_ch4 pregorder_ch5 pregorder_ch6 pregorder_ch7 pregorder_ch8 pregorder_ch9 pregorder_ch10 pregorder_ch11 pregorder_ch12 pregorder_ch13 pregorder_ch14 pregorder_ch15 pregorder_ch16)
gen numpreg=numpreg_p1
replace numpreg=0 if numpreg==. & female==1
replace numpreg=. if female==0
gen anyoffspring=1 if numpreg>0 & numpreg~=.
replace anyoffspring=0 if numpreg==0
gen age1preg2=(dob_ch1-dob)/365


gen anyoffspring_a8=anyoffspring if age2017>=8
gen numpreg_a8=numpreg if age2017>=8
gen numpreg_p1_a8=numpreg_p1 if age2017>=8 

local controls1 "cohort female primip"

gen MR_vs_SPR=1 if rear=="MR"
replace MR_vs_SPR=0 if rear=="PR"|rear=="SPR"

drop if cohort==. | female==. | primip==. | MR_vs_SPR==.


label var MR_vs_SPR "$\Psi(s_{*,1})-\Psi(s_{*,0})$" 

foreach var of varlist anyoffspring_a8 age1preg numpreg_a8 numpreg_p1_a8{
qui eststo r_fr_`var':teffects ra (`var' cohort primip) (MR_vs_SPR)

qui eststo r_fo_`var': reg `var' cohort primip MR_vs_SPR 
}

*********************
****** Table 4 ******
*********************
estout r_fo_* ,  keep(MR_vs_SPR)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "") eqlabel("" "") varlabels(MR_vs_SPR "$\Psi(s_{*,1})-\Psi(s_{*,0})$" )


*******************************
*** Birth Outcomes ***
*******************************
use "offspring_data.dta", replace


replace stillborn=0 if stillborn==.
foreach var of varlist bw diedb1m {
replace `var'=. if stillborn==1
}

gen livebirth=1-stillborn
gen liveda1m=1-diedb1m

local controls1 "cohort female primip"
local controls2 "mom_cohort mom_primip"
local controls3 "cohort female primip"

gen mom_MR_vs_SPR=1 if mom_rear=="MR"
replace mom_MR_vs_SPR=0 if mom_rear=="PR"|mom_rear=="SPR"

label var mom_MR_vs_SPR "$\Psi(s_{1,*})-\Psi(s_{0,*})$" 

drop if mom_cohort==. | female==. | mom_primip==. | cohort==. | primip==. |mom_MR_vs_SPR==.

foreach var of varlist livebirth liveda1m bw{
qui eststo r_br_`var':teffects ra (`var' mom_cohort mom_primip cohort female primip ) (mom_MR_vs_SPR), vce(cl mom_id)
qui eststo r_bo_`var': reg `var' mom_MR_vs_SPR mom_cohort mom_primip cohort female primip, vce(cl mom_id)
}

*********************
****** Table 5 ******
*********************
estout r_bo_* ,  keep(mom_MR_vs_SPR)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "") eqlabel("" "") varlabels(mom_MR_vs_SPR "$\Psi(s_{*,1})-\Psi(s_{*,0})$" )
























